//
//  ZZHomeTableViewController.swift
//  sina
//
//  Created by 张鹏 on 16/5/19.
//  Copyright © 2016年 ZP. All rights reserved.
//

import UIKit
import SVProgressHUD

private let userCellID = "userCellID"

class ZZHomeTableViewController: ZZBaseViewController {

    //定义数组模型
    var statuses: [ZZStatus] = [ZZStatus]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
     
     visitorView.updateUI("关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon", isHome: true)
        
        //调用setNavBar方法
        setNavBar()
        
        //调用注册的cell
        prepareForTableView()
        
        //加载数据
        loadData()
    }
    
    //设置导航条
    private  func setNavBar() {
    
        //创建一个按钮
        let btn = UIButton()
        
        //设置背景图片
        btn.setImage(UIImage(named: "navigationbar_pop"), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "navigationbar_pop"), forState: .Normal)
       btn.setImage(UIImage(named: "navigationbar_pop_highlighted"), forState: .Highlighted)
        
        let popItem = UIBarButtonItem(customView: btn)
        
        btn.addTarget(self, action: #selector(ZZHomeTableViewController.push), forControlEvents: .TouchUpInside)
        
        //自适应大小
        btn.sizeToFit()
        
    navigationItem.rightBarButtonItem = popItem
        
    }
    
    //按钮的点击方法
   @objc private func push() {
  
    //跳转页面
    let detail = ZZDetailViewController()
    
    //跳转之前隐藏底部的tabBar
    detail.hidesBottomBarWhenPushed = true
    
    //跳转
    self.navigationController?.pushViewController(detail, animated: true)
    }
    
    //注册cell
    func prepareForTableView() {
        
        //注册cell
        tableView.registerClass(ZZStatusTableViewCell.self, forCellReuseIdentifier: userCellID)
        
        //设置行高 ios 8.0之后的方法
//        tableView.rowHeight = UITableViewAutomaticDimension
        
        //设置预估行高,并且设置contentview的frame
        tableView.estimatedRowHeight = 300
        
        
    }
}


extension ZZHomeTableViewController {

    private func loadData() {
        
        //从网络加载数据
        
        ZZNetworkTools.sharedTools.loadHomePageData { (responseObject, error) in
            
            if error != nil {
                
                SVProgressHUD.showErrorWithStatus(noWIFI)
                return
            }
            
            //处理数据   AFN已经将数据处理成字典
            guard let dict = responseObject as? [String : AnyObject] else {
            return
            }
    
            //根据studus获取数组
            guard let array = dict["statuses"] as? [[String : AnyObject]] else {
            
            return
            }
           
            //实例化空数组
            var tempArray = [ZZStatus]()
            //for循环取出单个的array
            for item in array {
                //字典转模型
            let z = ZZStatus(dict: item)
            
                tempArray.append(z)

            }
            //赋值保存
            self.statuses = tempArray
            
            //刷新UI
            self.tableView.reloadData()
        }
    }

}


//数据源方法
extension ZZHomeTableViewController {
    
    //返回多少组
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    //返回有多少行
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        //首先要注册cell
        //强转为自定义的cell类型
        let cell = tableView.dequeueReusableCellWithIdentifier(userCellID, forIndexPath: indexPath) as!  ZZStatusTableViewCell
        
        //加载模型数据
       cell.status = statuses[indexPath.row]
        
        return cell
    }
    
}
















